<template>
  <div class="comment-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-table
        class="table-list"
        :data="articles"
        stripe
        size="mini"
        style="width: 100%"
        v-loading="loading">
        <el-table-column
          prop="title"
          label="标题">
        </el-table-column>
        <el-table-column
          prop="total_comment_count"
          label="总评论数">
        </el-table-column>
        <el-table-column
          prop="fans_comment_count"
          label="粉丝评论数">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#909399"
              :disabled="scope.row.disable"
              @change="onStatusChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :page-size="pageSize"
        :disabled="loading"
        :current-page.sync="currentPage"
        @current-change="onCurrentChange">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, updateCommentStatus } from '@/api/article.js'
export default {
  name: 'CommentIndex',
  components: {},
  props: {},
  data () {
    return {
      articles: [],
      loading: true,
      totalCount: 0,
      pageSize: 10,
      currentPage: 1
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadArticles()
  },
  mounted () {},
  methods: {
    onCurrentChange (page) {
      this.currentPage = page
      this.loadArticles()
    },
    loadArticles () {
      this.loading = true
      getArticles({
        page: this.currentPage,
        per_page: this.pageSize,
        response_type: 'comment'
      }).then(res => {
        const { results, total_count: totalCount } = res.data.data
        results.forEach(result => {
          result.disable = false
        })
        this.articles = results
        this.totalCount = totalCount
        this.loading = false
      })
    },
    onStatusChange (article) {
      article.disable = true
      updateCommentStatus(article.id.toString(), article.comment_status).then(res => {
        article.disable = false
        this.$message({
          message: article.comment_status ? '开启评论成功' : '关闭评论成功',
          type: 'success'
        })
      })
    }
  }
}

</script>
<style lang='less' scoped>
.table-list {
  margin-bottom: 30px;
}
</style>
